package com.ssm.changeunit.console.parser;

import com.ssm.changeunit.command.*;
import org.apache.commons.cli.*;

/**
 * 
 * @author 시리우스
 */
public class PushConsoleParser extends ConsoleParser {
    @Override
    public CommandWorkerOptions parse(java.lang.String[] userCommand) throws ConsoleParseException {
        try {
            // 파서 생성
            CommandLineParser parser = new GnuParser();

            Options options = new Options();

            Option serverOption = OptionBuilder.withLongOpt("server").hasArgs().isRequired().create();

            options.addOption(serverOption);

            CommandLine commandLine = parser.parse(options, userCommand);

            // item 프로퍼티가 있는지 검사
            if(commandLine.hasOption("server")) {
                CommandWorkerOptions commandWorkerOptions = new CommandWorkerOptions();
                CommandWorkerOption commandWorkerOption = new CommandWorkerOption("server");
                
                commandWorkerOptions.addCommandWorkerOption(commandWorkerOption);
                
                String server = commandLine.getOptionValue("server");

                commandWorkerOption.addValue(server);
                
                return commandWorkerOptions;
            }
            else {
                // 에러 발생
                throw new ConsoleParseException("server 프로퍼티가 존재하지 않습니다.");
            }
        }
        catch(ParseException ex) {
            throw new ConsoleParseException(ex);
        }
    }
}

